package cn.cloudreal.cbms.statements.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.cloudreal.cbms.castDetail.action.ArrangementAction;
import cn.cloudreal.cbms.statements.service.MediaCountService;
import cn.cloudreal.cbms.vo.MediaCount;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * Description:媒体数统计服务层视图类
 * 
 * Company:云恒瑞通
 * 
 * @author HuangCanCan
 *
 *@date 2012-08-15
 */
public class MediaCountAction extends ActionSupport
{
    private static final long serialVersionUID = 1L;
    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(ArrangementAction.class);
    
    /** 获得 MediaCountService对象用于提取方法*/
    private MediaCountService mediaCountService;

    /** 媒体表 */
    List<MediaCount> mediaCount;

    /** 获取年份 */
    private String years = null;

    /** 用于保存年度的list */
    private ArrayList<String> yearsList = null;

    /**
     * 获取媒体数统计
     */
    public String showMediaCount()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction initQueryArrangement() method, return String");
        }
        try
        {
            //定义list用于存放动态年份
            yearsList = new ArrayList<String>();
            //将获得的当前年份转化为int型
            int yeares = Integer.parseInt(this.getYear());
            yeares = yeares + 1;
            //当前年份之前及之前的五年放入list用于查询时的时间选择
            for (int i = 0; i < 5; i++)
            {
                yeares = yeares - 1;
                yearsList.add(String.valueOf(yeares));
            }
            // 查询时默认当前年份
            if (null == years || "".equals(years))
            {
                years = this.getYear();
            }
            // 查询
            Map condition = new HashMap();
            condition.put("i_year", years);
            mediaCount = mediaCountService.getMediaList(condition);
            resultName = SUCCESS;
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction initQueryArrangement() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }
        return resultName;
    }

    /**
     * 获取当前年份
     */
    public String getYear()
    {
        Calendar cal = Calendar.getInstance();
        //将获得日期转化成只有年份
        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        c.set(c.DATE, 1);
        return df.format(c.getTime());
    }

    public MediaCountService getMediaCountService()
    {
        return mediaCountService;
    }

    public void setMediaCountService(MediaCountService mediaCountService)
    {
        this.mediaCountService = mediaCountService;
    }

    public List<MediaCount> getMediaCount()
    {
        return mediaCount;
    }

    public void setMediaCount(List<MediaCount> mediaCount)
    {
        this.mediaCount = mediaCount;
    }

    public String getYears()
    {
        return years;
    }

    public void setYears(String years)
    {
        this.years = years;
    }

    public ArrayList<String> getYearsList()
    {
        return yearsList;
    }

    public void setYearsList(ArrayList<String> yearsList)
    {
        this.yearsList = yearsList;
    }

}
